package org.springframework.samples.petclinic

import java.lang.Integer
import java.util.Date
import java.util.HashSet
import scala.reflect.BeanProperty

import org.springframework.beans.support.MutableSortDefinition
import org.springframework.beans.support.PropertyComparator


class Pet(id:Integer, name:String, @BeanProperty var birthDate:Date, @BeanProperty var owner:Owner, var visits:java.util.Set[Visit])  extends NamedEntity(id, name) {
  
  var petType:PetType = null
  
  def this() = this(null, null, null, null, null) 

  def setType(petType:PetType) = this.petType = petType
  
  def getType = petType

  protected def setVisitsInternal(visits:java.util.Set[Visit]) = {
    this.visits = visits
  }

  protected def getVisitsInternal:java.util.Set[Visit] = {
    if (visits == null) {
      visits = new HashSet[Visit]
    }
    visits
  }
  
  def getVisits:java.util.List[Visit] = {
    var sortedVisits:java.util.List[Visit] = new java.util.ArrayList[Visit](getVisitsInternal)
    PropertyComparator.sort(sortedVisits, new MutableSortDefinition("date", false, false))
    java.util.Collections.unmodifiableList[Visit](sortedVisits)
  }

  def addVisit(visit:Visit) = {
    getVisitsInternal.add(visit)
    visit.pet = this
  }
  
}
